package com.lei.dao.impl;

import com.lei.dao.StaffDao;
import com.lei.entity.Staff;
import com.lei.util.BaseDb;
import com.lei.util.TransBean;

import java.util.List;
import java.util.Map;

/**
 * @author Lei Chu
 * @Date 2020/10/16 18:11
 */
public class StaffDaoImpl implements StaffDao {

    @Override
    public Staff checkLogin(Staff staff) {
        String sql = "select * from t_staff where staffId=? and password=?";
        List<Map> map = BaseDb.query(sql, staff.getStaffId(), staff.getPassword());
        if (map.size() > 0) {
            List list = TransBean.populate(Staff.class, map);
            return (Staff) list.get(0);
        }
        return null;
    }

    @Override
    public List<Staff> getAllStaff(Staff staff) {
        StringBuilder sql = new StringBuilder("select s.id,s.deptid,d.dname,s.staffId,s.staffName,s.phone,s.idCard,s.address,s.status,r.rname,s.createdTime,s.resignedTime,s.roleId from t_staff s,t_role r,t_dept d where s.roleId=r.rid and d.deptid=s.deptid and s.status=1 ");
        if (staff.getStaffId() != null) {
            sql.append(" and staffId = ").append(staff.getStaffId());
        }
        if (staff.getStaffName() != null) {
            sql.append(" and staffName like '%" + staff.getStaffName() + "%' ");
        }
        sql.append(" order by staffId desc limit ?,? ");
        List<Map> maps = BaseDb.query(String.valueOf(sql), (staff.getPage() - 1) * staff.getLimit(), staff.getLimit());
        if (maps.size() > 0) {
            return TransBean.populate(Staff.class, maps);
        }
        return null;
    }

    @Override
    public int getCount(Staff staff) {
        String sql = "select count(*) c from t_staff s,t_role r,t_dept d where s.roleId=r.rid and d.deptid=s.deptid and s.status = 1 ";
        if (staff.getStaffId() != null) {
            sql += " and staffId = " + staff.getStaffId();
        }
        if (staff.getStaffName() != null) {
            sql += " and staffName like '%" + staff.getStaffName() + "%' ";
        }
        sql += " order by staffId desc ";
        List<Map> maps = BaseDb.query(sql);
        return ((Long) maps.get(0).get("c")).intValue();

    }

    @Override
    public int getLastStaffId() {
        String sql = "select staffId from t_staff order by staffId desc limit 1";
        List<Map> result = BaseDb.query(sql);
        return (int) result.get(0).get("staffId");
    }

    @Override
    public int addStaff(Staff staff) {
        String sql = "insert into t_staff values(null,?,?,?,?,?,?,?,null,?,?,?)";
        return BaseDb.update(sql, staff.getStaffId(), staff.getStaffName(), staff.getPassword(), staff.getPhone(), staff.getIdCard(), staff.getAddress(), staff.getCreatedTime(), staff.getStatus(), staff.getRoleId(), staff.getDeptid());
    }

    @Override
    public int updateStaff(Staff staff) {
        String sql = "update t_staff set staffName=?,phone=?,idCard=?,address=?,status=?,roleId=?,deptid=? where id = ?";
        return BaseDb.update(sql, staff.getStaffName(), staff.getPhone(), staff.getIdCard(), staff.getAddress(), staff.getStatus(), staff.getRoleId(), staff.getDeptid(), staff.getId());
    }

    @Override
    public int updatePassword(Staff staff) {
        String sql = "update t_staff set password=? where staffId=?";
        return BaseDb.update(sql, staff.getPassword(), staff.getStaffId());
    }

    @Override
    public int deleteStaff(int id) {
        String sql = "delete from t_staff where id = ?";
        return BaseDb.update(sql, id);
    }

    @Override
    public List<Staff> getAllResignedStaff(Staff staff) {
        StringBuilder sql = new StringBuilder("select s.id,s.deptid,d.dname,s.staffId,s.staffName,s.phone,s.idCard,s.address,s.status,r.rname,s.createdTime,s.resignedTime,s.roleId from t_staff s,t_role r,t_dept d where s.roleId=r.rid and d.deptid=s.deptid and s.status= 0 ");
        if (staff.getStaffId() != null) {
            sql.append(" and staffId = ").append(staff.getStaffId());
        }
        if (staff.getStaffName() != null) {
            sql.append(" and staffName like '%" + staff.getStaffName() + "%' ");
        }
        sql.append(" order by staffId desc limit ?,? ");
        List<Map> maps = BaseDb.query(String.valueOf(sql), (staff.getPage() - 1) * staff.getLimit(), staff.getLimit());
        if (maps.size() > 0) {
            return TransBean.populate(Staff.class, maps);
        }
        return null;
    }

    @Override
    public int getResignedCount(Staff staff) {
        String sql = "select count(*) c from t_staff s,t_role r,t_dept d where s.roleId=r.rid and d.deptid=s.deptid and s.status = 0 ";
        if (staff.getStaffId() != null) {
            sql += " and staffId = " + staff.getStaffId();
        }
        if (staff.getStaffName() != null) {
            sql += " and staffName like '%" + staff.getStaffName() + "%' ";
        }
        sql += " order by staffId desc ";
        List<Map> maps = BaseDb.query(sql);
        return ((Long) maps.get(0).get("c")).intValue();

    }

    @Override
    public int staffResigned(String id, String resignedTime) {
        String sql = "update t_staff set resignedTime = ?,status = 0 where id = ?";
        return BaseDb.update(sql, resignedTime, id);
    }

    @Override
    public int staffCreated(String id, String createdTime) {
        String sql = "update t_staff set resignedTime = null, createdTime = ?,status = 1 where id = ?";
        return BaseDb.update(sql, createdTime, id);
    }

    @Override
    public List<Map> getNowStaffById(Integer id) {
        String sql = "select * from t_staff s join t_dept d on s.deptid = d.deptid join t_role r on r.rid = s.roleId where s.id = ?";
        return BaseDb.query(sql,id);
    }

    @Override
    public int updateStaffPhoneAndAdderss(Staff staff) {
        String sql = "update t_staff set address = ?,phone = ? where id = ?";
        return BaseDb.update(sql,staff.getAddress(),staff.getPhone(),staff.getId());
    }

    @Override
    public int updateUrl(String url, Integer id) {
        String sql = "update t_staff set url = ? where id = ?";
       return BaseDb.update(sql,url,id);
    }

}
